/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.ArrayList;
import model.Binhluansanpham;
import model.Sanpham;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import util.HibernateUtil;

/**
 *
 * @author HoangLiem
 */
public class BinhLuanSanPhamDAO {

    public static ArrayList<Binhluansanpham> getAllComments() {
        ArrayList<Binhluansanpham> list = new ArrayList<Binhluansanpham>();
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            Transaction trans = session.getTransaction();
            trans.begin();
            String hql = "from Binhluansanpham";
            Query query = session.createQuery(hql);
            list = (ArrayList<Binhluansanpham>) query.list();
            trans.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return list;
    }

    public static ArrayList<Binhluansanpham> getAllCommentsByProduct(Sanpham sanpham) {

        ArrayList<Binhluansanpham> list = new ArrayList<Binhluansanpham>();
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            Transaction trans = session.getTransaction();
            trans.begin();
            String hql = "from Binhluansanpham bl where bl.sanpham = :sanpham";
            Query query = session.createQuery(hql);
            query.setEntity("sanpham", sanpham);
            list = (ArrayList<Binhluansanpham>) query.list();
            trans.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return list;
    }

    public static boolean insertComment(Binhluansanpham bl) {
        boolean result = false;
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            Transaction trans = session.getTransaction();
            trans.begin();
            session.save(bl);
            result = true;
            trans.commit();
        } catch (Exception ex) {
            ex.printStackTrace();
            result = false;
        }
        return result;
    }
}
